package de.elmar_baumann.nb.slclock.timer;

import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.Icon;
import javax.swing.SwingUtilities;
import org.netbeans.api.annotations.common.StaticResource;
import org.openide.util.ImageUtilities;

/**
 * @author Elmar Baumann
 */
public class TimerDisplayPanel extends javax.swing.JPanel {

    private static final long serialVersionUID = 1L;
    @StaticResource private static final String ICON_PATH_TIMER_RUNS = "de/elmar_baumann/nb/slclock/icons/timer-runs.png";
    @StaticResource private static final String ICON_PATH_TIMER_PAUSES = "de/elmar_baumann/nb/slclock/icons/timer-pauses.png";
    private static final Icon ICON_TIMER_RUNS = ImageUtilities.loadImageIcon(ICON_PATH_TIMER_RUNS, false);
    private static final Icon ICON_TIMER_PAUSES = ImageUtilities.loadImageIcon(ICON_PATH_TIMER_PAUSES, false);

    public TimerDisplayPanel() {
        initComponents();
        setIcon();
        addMouseListener(preferencesDialogDisplayer);
        TimerEventsModel.getInstance().addPropertyChangeListener(iconUpdater);
        labelTimer.addMouseListener(preferencesDialogDisplayer);
        setVisible(TimerEventsModel.getInstance().isShowIcon());
    }

    private void setIcon() {
        labelTimer.setIcon(TimerEventsModel.getInstance().getRunningEventsCount() > 0
                ? ICON_TIMER_RUNS
                : ICON_TIMER_PAUSES);
    }

    private final PropertyChangeListener iconUpdater = new PropertyChangeListener() {
        @Override
        public void propertyChange(final PropertyChangeEvent evt) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    String propertyName = evt.getPropertyName();
                    if (TimerEventsModel.PROPERTY_EVENTS.equals(propertyName)) {
                        setIcon();
                    } else if (TimerEventsModel.PROPERTY_SHOW_ICON.equals(propertyName)) {
                        setVisible((boolean) evt.getNewValue());
                    }
                }
            });
        }
    };

    private final MouseListener preferencesDialogDisplayer = new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e)) {
                TimerEventsModel.getInstance().showSettingsGui();
            }
        }
    };

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelTimer = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        labelTimer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/de/elmar_baumann/nb/slclock/icons/timer-pauses.png"))); // NOI18N
        add(labelTimer, new java.awt.GridBagConstraints());
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel labelTimer;
    // End of variables declaration//GEN-END:variables

}
